package top.hkyzf.study.leetcode.string;

import org.junit.Test;

import java.util.Deque;
import java.util.LinkedList;

/**
 * description:
 *
 * @author zhufeng
 * @version 1.0
 * @date 2024-3-31 14:43
 */
public class 验证二叉树的前序序列化_331 {
    public boolean isValidSerialization(String preorder) {
        String[] strs = preorder.split(",");
        if (preorder.equals("#")) {
            return true;
        }
        if (strs[0].equals("#")) {
            return false;
        }
        int slot = 2;
        for (int i = 1; i < strs.length; i++) {
            if (strs[i].equals("#")) {
                slot--;
            } else {
                slot++;
            }
            if (slot < 0) {
                return false;
            }
            if (slot == 0 && i != strs.length-1) {
                return false;
            }
        }
        return slot == 0;
    }

    @Test
    public void testIsValid () {
        String preorder = "7,2,#,2,#,#,#,6,#";
        System.out.println(isValidSerialization(preorder));
    }
}
